package http;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.ContentType;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class HutoolHttp {

    public static void main(String[] args) {

        Map<String,Object> maps = new HashMap<>();
        maps.put("ask","单位用户包括哪些类别");
        maps.put("conversation",new ArrayList<>());

        HttpRequest request = HttpRequest.post("http://10.19.151.57:8100" +"/sse/chat");
        request.body(JSONUtil.toJsonStr(maps), ContentType.JSON.getValue());
        request.timeout(1000 * 60);
        HttpResponse response = request.executeAsync();
        StringBuilder res = new StringBuilder();
//        ChatChunkTextMsgItem chatChunkTextMsgItem = new ChatChunkTextMsgItem(askId, "");
//        R<ChatChunkTextMsgItem> rst = R.successWithData(chatChunkTextMsgItem);
        try (InputStream stream = response.bodyStream()) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
            String line;
            while ((line = reader.readLine()) != null) {
                log.info("line->{}",line);
                if (StrUtil.isNotBlank(line)) {
                VerbaSSeEntity verbaSSeEntity = JSONUtil.toBean(line.substring(6).trim(), VerbaSSeEntity.class);
                    System.out.println(JSONUtil.toJsonStr(verbaSSeEntity));
//                    VerbaSSeEntity verbaSSeEntity = JSONUtil.toBean(line.trim(), VerbaSSeEntity.class);
//                    String message = verbaSSeEntity.getMessage();
//                    if (!"stop".equalsIgnoreCase(message)) {
//                        res.append(message);
//                        chatChunkTextMsgItem.setContent(message);
//                        SseEmitterUtil.sendMessage(channel, rst);
//                    }
                }
            }
        } catch (IOException e) {
            log.error("获取io流失败!", e);
            e.printStackTrace();
//            throw new RCException(channel + " 获取io流失败!");
        } finally {

        }


    }

}
